package com.asm.shiroweb.service;

import com.asm.shiroweb.common.bean.UserBean;
import com.asm.shiroweb.common.util.TestData;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserService {

    // 根据用户名 获取用户
    public UserBean queryUserByName(final String username){
        final UserBean userBean;
        final List<UserBean> queryUsers =
                        TestData.allUsers
                        .stream()
                        .filter(u -> u.getUserName().equals(username))
                        .collect(Collectors.toList());

        if (queryUsers != null && queryUsers.size()>0) {

            try {
                userBean = (UserBean) BeanUtils.cloneBean(queryUsers.get(0));

                return userBean;
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return null;
    }
    // 根据手机号 获取用户
    public UserBean queryUserByMobile(final String mobile){
        final UserBean userBean;
        final List<UserBean> queryUsers =
                        TestData.allUsers
                        .stream()
                        .filter(u -> u.getUserMobile().equals(mobile))
                        .collect(Collectors.toList());

        if (queryUsers != null && queryUsers.size()>0) {

            try {
                userBean = (UserBean) BeanUtils.cloneBean(queryUsers.get(0));

                return userBean;
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return null;
    }


}
